#!/usr/bin/python           # This is client.py file

"""
 UDP client
"""

import socket           # Import socket module


host = socket.gethostname()        # Get local machine name
port = 8080               # Reserve a port for your service.

with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:

    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)  
    s.connect((host, port)) # once it connects, c, addr = s.accept() will be executed

    while True:
        w = input('Input: ')
        print('[sending...]')

        try:
            s.send(w.encode())
            print('[success]')
        except Exception as ex:
            print('fail to send')
        print('[receiving...]')
        rd = s.recv(1024)      # wait to receive the message from server.py

        print('[received]')
        print(rd,'[received from server]')
        if rd == 'quit':
            print('the service is quitted')
            break

